package com.stylefeng.guns.modular.vpn_ads_manage.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.vpn.utils.TimeUtil;
import com.stylefeng.guns.modular.vpn_ads_manage.dto.AdsManageDTO;
import com.stylefeng.guns.modular.vpn_ads_manage.entity.AdsEntity;
import com.stylefeng.guns.modular.vpn_ads_manage.entity.AdsItemBO;
import com.stylefeng.guns.modular.vpn_ads_manage.service.IVpnAdsManageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;

/**
 * 广告位管理
 */
@Controller
@RequestMapping("/vpnAdsManage")
public class VpnAdsManageController extends BaseController {

    private String PREFIX = "/vpn_ads_manage/";

    @Autowired
    private IVpnAdsManageService manageService;

    /**
     * 跳转到广告位管理列表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "vpnAdsManage.html";
    }

    /**
     * 跳转到广告位修改页面
     * @param adId
     * @param model
     * @return
     */
    @RequestMapping("/vpnAdsManage_update/{adId}")
    public String vpnExclusiveLineUpdate(@PathVariable Integer adId, Model model) {
        AdsItemBO itemBO = manageService.selectOneById(adId);

        AdsEntity item = new AdsEntity();
        BeanUtils.copyProperties(itemBO,item);
        item.setStartTime(TimeUtil.dateToGTMString(itemBO.getStartTime(),"yyyy-MM-dd HH:mm:ss"));
        item.setEndTime(TimeUtil.dateToGTMString(itemBO.getEndTime(),"yyyy-MM-dd HH:mm:ss"));
        model.addAttribute("item",item);
        LogObjectHolder.me().set(item);
        return PREFIX + "vpnAdsManage_update.html";
    }

    /**
     * 广告位列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(AdsManageDTO dto) {
        return manageService.list(dto);
    }

    /**
     * 修改广告位
     * @return
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(AdsManageDTO dto) {
        manageService.update(dto);
        return SUCCESS_TIP;
    }

    /**
     * 改变广告位状态
     * @return
     */
    @RequestMapping(value = "/changeAdsStatus")
    @ResponseBody
    public Object changeAdsStatus(@RequestParam("adId") Integer adId,
                                  @RequestParam("status") Integer status) {
        return manageService.changeStatus(adId,status);
    }

    /**
     * 导出表格
     * @param adName
     * @param adLocation
     * @param startTime
     * @param endTime
     * @param showType
     * @param status
     * @param response
     * @return
     */
    @RequestMapping(value = "/export")
    @ResponseBody
    public void export(@RequestParam("adName") String adName,
                         @RequestParam("adLocation") String adLocation,
                         @RequestParam("startTime") String startTime,
                         @RequestParam("endTime") String endTime,
                         @RequestParam("showType") String showType,
                         @RequestParam("status") String status,
                         HttpServletResponse response) {
        manageService.export(response,adName,adLocation,startTime,endTime,showType,status);
    }
}
